<?php

namespace app\proxy\model\proxy;

use app\common\model\proxy\Region as RegionModel;
use app\common\model\region\Region as Region1;

class Region extends RegionModel
{

    public static function getRegionsInfo($roxyId)
    {
        $list = static::withoutGlobalScope()
            ->where("is_delete", '=', 0)
            ->where("proxy_id", '=', $roxyId)
            ->field(['proxy_region_id', 'province_id', 'city_id', 'county_id', 'region_id'])
            ->select();
        $regions = [];
        foreach ($list as $item) {
            $regions[$item["region_id"]] = [
                'province_id' => $item["province_id"],
                'city_id' => $item["city_id"],
                'county_id' => $item["county_id"],
                'region_id' => $item["region_id"],
                'path' => array_filter([$item["province_id"], $item["city_id"], $item['county_id']], function ($item) {
                    return $item && $item > 0;
                }),
                'name' => Region1::getMergerName($item["region_id"])
            ];
        }
        return $regions;
    }

    public static function getRegionIds($roxyId)
    {
        $list = static::withoutGlobalScope()
            ->where("is_delete", '=', 0)
            ->where("proxy_id", '=', $roxyId)
            ->field(['region_id'])
            ->hidden()
            ->select();
        $regions = [];
        foreach ($list as $item) {
            $regions[] = $item["region_id"];
        }
        return $regions;
    }

    public static function getProxyName($data, $proxy_id)
    {
        if (empty($data["proxy_id"])) {
            return '-';
        }
        if ($data["proxy_id"] == $proxy_id) return "【我】";

        $user = (new User())->find($data["proxy_id"]);

        return $user["user_name"] ?? "";

//        $proxy_region = static::withoutGlobalScope()->with(["user"])
//            ->where("is_delete", '=', 0)
//            ->where(function ($query) use ($data) {
//                $query->where("province_id", $data["province_id"])->whereOr("city_id", $data["city_id"])->whereOr('county_id', $data["county_id"]);
//            })
//            ->order("level", "desc")
//            ->find();
//        if (empty($proxy_region)) return "";
//        if ($proxy_region["proxy_id"] == $proxy_id) return "【我】";
//        return $proxy_region["user"]["user_name"] ?? "";

    }

    public static function regionListOptions($roxyId, $disabled = false, $getlvs = true)
    {
        $list = static::withoutGlobalScope()
            ->where("is_delete", '=', 0)
            ->where("proxy_id", '=', $roxyId)
            ->field(['proxy_region_id', 'province_id', 'city_id', 'county_id', 'region_id'])
            ->select();
        $model = new Region1;
        $allRegions = $model->where('is_delete', '=', '0')->select()->toArray();
        $allRegionsKV = [];
        foreach ($allRegions as $region) {
            $allRegionsKV[$region["id"]] = $region;
        }
        $options = [];
        foreach ($list as $item) {
            if ($item["county_id"]) {
                $allRegionsKV[$item["province_id"]]["disabled"] = $disabled;
                $allRegionsKV[$item["city_id"]]["disabled"] = $disabled;
                $options[] = $allRegionsKV[$item["province_id"]];
                $options[] = $allRegionsKV[$item["city_id"]];
                $options[] = $allRegionsKV[$item["county_id"]];
            } elseif ($item["city_id"]) {
                $allRegionsKV[$item["province_id"]]["disabled"] = $disabled;
                $options[] = $allRegionsKV[$item["province_id"]];
                $options[] = $allRegionsKV[$item["city_id"]];
                foreach ($allRegionsKV as $region) {
                    if ($region["pid"] == $item["city_id"]) {
                        $options[] = $region;
                    }
                }
            } else {
                $options[] = $allRegionsKV[$item["province_id"]];
                foreach ($allRegionsKV as $region) {
                    if ($region["pid"] == $item["province_id"]) {
                        $options[] = $region;
                        foreach ($allRegionsKV as $region2) {
                            if ($region2["pid"] == $region["id"]) {
                                $options[] = $region2;
                            }
                        }
                    }
                }
            }
        }
        return get_tree_children($options,'children','id','pid',$getlvs);
    }

    public static function checkRegion($province_id, $city_id, $county_id, $proxy_id)
    {

    }

    public static function cityListOptions($roxyId)
    {
        $list = static::withoutGlobalScope()
            ->where("is_delete", '=', 0)
            ->where("proxy_id", '=', $roxyId)
            ->where("level", "=", 2)
            ->field(['proxy_region_id', 'province_id', 'city_id'])
            ->select();
        if (empty($list)) return [];

        $options = [];
        $model = new Region1;
        foreach ($list as $item) {
            if (empty($options[$item["province_id"]])) {
                $options[$item["province_id"]] = $model->find($item["province_id"]);
            }
            if (empty($options[$item["city_id"]])) {
                $options[$item["city_id"]] = $model->find($item["city_id"]);
            }
        }
        return get_tree_list($options);

    }

}